<template>
  <div class="account">
    <div class="accountadd-wrap">
      <h1>账号添加</h1>
      <div class="add-cont">
        <el-form
          :rules="rules"
          label-width="80px"
          status-icon
          :model="formData"
          ref="formRef"
        >
          <el-form-item label="账号" prop="account" class="account">
            <el-input v-model="formData.account"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password" class="password">
            <el-input v-model="formData.password"></el-input>
          </el-form-item>
          <el-form-item label="用户组">
            <el-select v-model="formData.userGroup" class="user">
              <el-option label="超级管理员" value="超级管理员"></el-option>
              <el-option label="普通管理员" value="普通管理员"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button @click="submit" type="primary">添加</el-button>
            <el-button @click="reset" type="default">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import { addUserReq } from '@/api/user';

const checkAccount = (rule, value, callback) => {
  if (!value) {
    callback(new Error("账号不能位空"));
  } else if (!/^[\u4E00-\u9FA5a-zA-Z0-9]{4,16}$/.test(value)) {
    callback(new Error("数字,英文,中文4-16位"));
  } else {
    callback();
  }
};
const checkPassword = (rule, value, callback) => {
  //非空验证
  if (!value) {
    callback(new Error("密码不能为空"));
    //正则验证
  } else if (!/^[a-zA-Z0-9_-]{4,16}$/.test(value)) {
    callback(new Error("数字,英文4-16位"));
  } else {
    callback();
  }
};
export default {
  data() {
    return {
      formData: {
        account: "",
        password: "",
        userGroup: "",
      },
      //验证规则
      rules: {
        account: [{ validator: checkAccount, trigger: "blur" }],
        password: [{ validator: checkPassword, trigger: "blur" }],
        userGroup: [
          { required: true, message: "请选择用户组", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    //提交表单
    submit() {
      this.$refs.formRef.validate( async valid => {
        console.log("valid :>> ", valid);
        if (valid) {
          let res = await addUserReq(this.formData)
           //解构数据
          let {code, msg} = res.data
          //判断
          if (code === 0) {
            //跳转
            this.$router.push('/account')
          }
        }
      });
    },
    //重置表单
    reset() {
      this.$refs.formRef.resetFields();
    },
  },
};
</script>

<style lang="scss" scoped>
h1 {
  color: #e3bc25;
  font-size: 23px;
  height: 55px;
  padding-top: 15px;
  padding-left: 10px;
  border-bottom: 1px solid #e3bc25;
  margin-bottom: 20px;
}
.accountadd-wrap {
  background-color: #fef8f8;
  width: 600px;
  margin-top: 30px;
  padding-bottom: 30px;
  margin-left: 30px;
}
.account {
  width: 450px;
  padding-left: 0;
}
.password {
  width: 450px;
}
.user {
  width: 370px;
}
.el-button--primary {
  background-color: #e3bc25;
  border: 0;
  font-size: 16px;
}
</style>
